package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;
    @Override
    public void check(Long id) {
        //要想删除先通过MP框架下的查询方法，查询分类下有没有关联，如果有判处异常，如果没有正常删除
        //先对菜品进行判断
        //要想查询关联菜品需要对应的对象，通过自动装配导入对象
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        //查询个数
        int count = dishService.count(dishLambdaQueryWrapper);
        //查询个数，将之前的查询作为条件放入count里
        if (count>0){
            //说明分类下有关联菜品，无法删除，自定义业务异常，并进行抛出
            throw new CustomException("当前分类下有关联菜品，无法删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        //查询个数，将之前的查询作为条件放入count里
        int count1 = setmealService.count(setmealLambdaQueryWrapper);
        //判断
        if (count1>0){
            //说明分类下有关联菜品，无法删除，自定义业务异常，并进行抛出
            throw new CustomException("当前分类下有关联套餐，无法删除");

        }
        //进行到这一步，说明都没有关联，正常删除，调用父类删除方法
        super.removeById(id);
    }
}
